package com.jason.fingerprint.store;

import android.net.Uri;
import android.os.Environment;
import com.jason.fingerprint.beans.ui.FingerPrintBean;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class GlobalData {
    public static List<FingerPrintBean> FingerList;
//	public static List<Record> recordList;

    private static String sWorkDir = "/sdcard/FGTIT";
    private static String sUsFile = "userslist.xml";
    private static List<Uri> fsList;

    public static boolean IsHaveSdCard() {
        String status = Environment.getExternalStorageState();
        if (status.equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

    public static void CreateDir() {
        if (IsHaveSdCard()) {
            File destDir = new File(sWorkDir);
            if (!destDir.exists()) {
                destDir.mkdirs();
            }
        } else {
        }

//    	RecordFile.CreateFile(sWorkDir+"/"+sRsFile);
    }

    public static void LoadFileList() {
        File file = new File(sWorkDir);

        fsList = new ArrayList<Uri>();
        if (file.isDirectory()) {
            File[] all_file = file.listFiles();
            if (all_file != null) {
                for (int i = 0; i < all_file.length; i++) {
                    fsList.add(Uri.parse(all_file[i].toString()));
                }
            }
        }
    }

    public static boolean FileIsExists(Uri fsname) {
        for (int i = 0; i < fsList.size(); i++) {
            if (fsList.get(i).equals(fsname))
                return true;
        }
        return false;
    }

    public static void LoadUsersList() {
        FingerList = new ArrayList<FingerPrintBean>();
        if (FileIsExists(Uri.parse(sWorkDir + "/" + sUsFile))) {
            List<FingerPrintBean> Fingeritems = FingerXmlParse.parseFingerList(XmlParase.ReadXmlFile(sWorkDir + "/" + sUsFile));
            for (int i = 0; i < Fingeritems.size(); i++) {
                FingerList.add(Fingeritems.get(i));
            }
            //Toast.makeText(MainActivity.this, "Count:"+String.valueOf(FingerList.size()), Toast.LENGTH_SHORT).show();
        }
    }

    public static boolean IsHaveFinger(int id) {
        for (int i = 0; i < FingerList.size(); i++) {
            if (FingerList.get(i).id == (id))
                return true;
        }
        return false;
    }

    public static FingerPrintBean FindFingerByCard(String card) {
        for (int i = 0; i < FingerList.size(); i++) {
            if (FingerList.get(i).cardNo.equals(card))
                return FingerList.get(i);
        }
        return null;
    }

    public static FingerPrintBean FindFingerByID(int id) {
        for (int i = 0; i < FingerList.size(); i++) {
            if (FingerList.get(i).id == (id))
                return FingerList.get(i);
        }
        return null;
    }

    public static void SaveUsersList() {
        XmlParase.WriteXmlFile(FingerXmlParse.parseFingerToXml(FingerList), sWorkDir + "/" + sUsFile, "utf-8");
    }

    public static void DeleteFile(String filename) {
        File f = new File(filename);
        if (f.exists()) {
            f.delete();
        }
    }
}
